<template>
  <el-form class="select-wrapper" label-width="40px" label-position="top">
    <el-form-item label="Row">
      <el-input v-model.number="row" />
    </el-form-item>
    <el-form-item label="Col">
      <el-input v-model.number="col" />
    </el-form-item>
    <el-form-item label="选中元素">
      <el-image shape="square" fit="fill" :src="TilesImage[activeElement!]">
        <template #error>未选择</template>
      </el-image>
      <el-button
        @click="removeCommonElement"
        :disabled="!activeElement"
        type="danger"
        :icon="Delete"
        circle
      />
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { watchEffect } from 'vue';
import { RoleTiles, TilesImage } from '@/constants';
import { useMapStore } from '@/stores/common/map';
import { useSelectStore } from '@/stores/editMap/elementSelect';
import { Delete } from '@element-plus/icons-vue';
import { useElementRenderStore } from '@/stores/common/elementRender';
import { ElMessageBox } from 'element-plus';
import { errorMessage, successMessage } from '@/utils';
const { row, col, updateMapCol, updateMapRow } = $(useMapStore());
const { activeElement } = $(useSelectStore());

const { removeItemById } = useElementRenderStore();

function removeCommonElement() {
  if (activeElement === RoleTiles.HERO) {
    return errorMessage('英雄元素不能删除');
  }
  ElMessageBox.confirm('删除地图中所有同类元素?', 'Warning', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    activeElement && removeItemById(activeElement);
    successMessage('删除成功');
  });
}

watchEffect(() => {
  if (col > 0) {
    updateMapCol();
  }
});

watchEffect(() => {
  if (row > 0) {
    updateMapRow();
  }
});
</script>

<style lang="scss" scoped>
:deep(.el-form-item) {
  .el-form-item__label {
    font-weight: 800;
  }

  .el-form-item__content {
    justify-content: space-between;
  }
}
</style>
